package com.kedacom.emptyproject.map.activity

import android.os.Bundle
import android.support.constraint.ConstraintLayout
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.view.View
import com.kedacom.emptyproject.MainApplication
import com.kedacom.emptyproject.R
import com.kedacom.emptyproject.databinding.ActivityMapLayerBinding
import com.kedacom.emptyproject.map.adapter.MyFragmetnPageAdapter
import com.kedacom.emptyproject.map.behavior.MyBottomSheetBehavior
import com.kedacom.emptyproject.map.fragment.IndexFragment1
import com.kedacom.emptyproject.map.fragment.IndexFragment2
import com.kedacom.emptyproject.map.fragment.IndexFragment3
import com.kedacom.emptyproject.map.viewmodel.MapLayerViewModel
import com.kedacom.emptyproject.map.widget.MapLayerBottomTabView
import com.kedacom.emptyproject.util.dp2px
import com.kedacom.lego.annotation.ContentView
import com.kedacom.lego.annotation.ViewModel
import com.kedacom.widget.common.StatusBarUtil
import kotlinx.android.synthetic.main.activity_map_layer.*
import kotlin.system.exitProcess


@ContentView(R.layout.activity_map_layer)
@ViewModel(MapLayerViewModel::class)
class MapLayerActivity : BaseActivity<ActivityMapLayerBinding, MapLayerViewModel>(),
    MyBottomSheetBehavior.BottomSheetCallback, MapLayerBottomTabView.OnBotViewClickListener {
    private var behavior: MyBottomSheetBehavior<ConstraintLayout>? = null
    private var previousTime = 0L//上一次按返回键的时间
    private val quiteTimeGap = 2000//连续按返回键退出的时间间隔
    private var layerInfoNumState: Int? = null
    val fragments = ArrayList<Fragment>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initMap()
        initView()
        initShowModule()
    }

    private fun initView() {
        fragments.add(IndexFragment1())
        fragments.add(IndexFragment2())
        fragments.add(IndexFragment3())

        kmapViewPager.adapter = MyFragmetnPageAdapter(supportFragmentManager, fragments)
        kmapViewPager.offscreenPageLimit = 5
        kmapViewPager.currentItem = 1

        //切换页面动态改变页面高度
        kmapViewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(p0: Int) {
            }

            override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {
            }

            override fun onPageSelected(p0: Int) {
                var paddingBottom: Int = dp2px(70f).toInt()
                var darkTheme = false
                if (fragments[p0] is IndexFragment2) {
                    paddingBottom = 0
                    darkTheme = true
                }
                StatusBarUtil.statusBarMode(this@MapLayerActivity, darkTheme)//设置深色字体

                kmapViewPager.setPadding(
                    kmapViewPager.paddingLeft,
                    kmapViewPager.paddingTop,
                    kmapViewPager.paddingRight,
                    paddingBottom
                )
            }

        })
    }

    private fun initMap() {
        behavior = MyBottomSheetBehavior.from(clBeahvior)
        behavior?.setBottomSheetCallback(this)

        layer_info_num.setOnClickListener {
            if (behavior?.getState() == MyBottomSheetBehavior.STATE_COLLAPSED) {
                myBitmapAnim.expanded()
                bottomTabView?.showStatus(MapLayerBottomTabView.LayerStatus.NORMAL)
            }
        }
        bottomTabView.setBotViewClickListener(this)
    }

    //初始化显示哪些模块 警情信息模块 要情信息模块 指令信息模块 police_center/demand_center/order_center
    private fun initShowModule() {
        //注意：根据显示的模块，设置UserLoginMode的值，动态计算中间部分的偏移量
        val havePoliceCenter = true
        val haveDemandCenter = false
        val haveOrderCenter = false

        nViewDataBinding.includeTab.apply {
            if (havePoliceCenter) {
                MainApplication.UserLoginMode = 1
            }
            if (haveDemandCenter) {
                layoutLeft.visibility = View.VISIBLE
                MainApplication.UserLoginMode = 1
            }
            if (haveOrderCenter) {
                layoutRight.visibility = View.VISIBLE
                MainApplication.UserLoginMode = 1
            }

            if (haveDemandCenter && haveOrderCenter) {
                lineVertical.visibility = View.VISIBLE
            }

            if (havePoliceCenter && (haveDemandCenter || haveOrderCenter)) {
                lineTop.visibility = View.VISIBLE
                MainApplication.UserLoginMode = 2
            }
        }
    }

    override fun onSlide(bottomSheet: View, midOffset: Float, slideOffset: Float) {
        coordinator.setPoi(midOffset, slideOffset)
    }

    override fun onStateChanged(bottomSheet: View, newState: Int) {
        super.onStateChanged(bottomSheet, newState)
        if (myBitmapAnim.isAnim())
            return
        when (newState) {
            MyBottomSheetBehavior.STATE_COLLAPSED -> {
                if ((kmapViewPager.adapter as MyFragmetnPageAdapter).getItem(mBinding.kmapViewPager.currentItem) is IndexFragment2) {
                    bottomTabView.showStatus(MapLayerBottomTabView.LayerStatus.CLOSE)
                    myBitmapAnim.showAlarmNum()
                }
            }
        }
    }

    /**
     * 点击主页左侧
     */
    override fun botViewLeft() {
        if (myBitmapAnim.isAnim())
            return
        kmapViewPager.currentItem = 0
        mBinding.kmapViewPager.currentItem = 0
        mBinding.location.visibility = View.GONE
        mBinding.layerInfoNum.visibility = View.GONE
        bottomTabView.showStatus(MapLayerBottomTabView.LayerStatus.NORMAL)
        bottomTabView.setClickLeftBtm()
        behavior?.setState(MyBottomSheetBehavior.STATE_COLLAPSED)
    }

    /**
     * 点击主页右侧
     */
    override fun botViewRight() {
        if (myBitmapAnim.isAnim())
            return
        kmapViewPager.currentItem = fragments.size - 1
        mBinding.kmapViewPager.currentItem = fragments.size - 1
        mBinding.location.visibility = View.GONE
        mBinding.layerInfoNum.visibility = View.GONE
        bottomTabView.showStatus(MapLayerBottomTabView.LayerStatus.NORMAL)
        bottomTabView.setClickRightBtm()
        behavior?.setState(MyBottomSheetBehavior.STATE_COLLAPSED)
    }

    /**
     * 点击主页（中心）
     * @param currentState Int
     */
    override fun homeClick(@MapLayerBottomTabView.LayerStatus currentState: Int) {
        bottomTabView.setClickHomeBtm()
//        if (tabViewState.size == 2) {
//            if (tabViewState.indexOf("2") == 0)
//                bottomTabView.setClickLeftBtm()
//            if (tabViewState.indexOf("2") == 1)
//                bottomTabView.setClickRightBtm()
//        }
        if ((kmapViewPager.adapter as MyFragmetnPageAdapter).getItem(mBinding.kmapViewPager.currentItem) !is IndexFragment2) {
            bottomTabView.showStatus(MapLayerBottomTabView.LayerStatus.NORMAL)
            mBinding.clBeahvior.visibility = View.VISIBLE
            behavior?.setState(MyBottomSheetBehavior.STATE_MIDDLE)
            mBinding.kmapViewPager.currentItem = 1
            mBinding.location.visibility = View.VISIBLE
            return
        }
        if (myBitmapAnim.isAnim())
            return
        if (behavior?.getState() == MyBottomSheetBehavior.STATE_EXPANDED) {
            behavior?.setState(MyBottomSheetBehavior.STATE_MIDDLE)
            return
        }
        when (currentState) {
            MapLayerBottomTabView.LayerStatus.CLOSE -> {
                myBitmapAnim.close()
                bottomTabView.showStatus(MapLayerBottomTabView.LayerStatus.CLOSE)
            }
            MapLayerBottomTabView.LayerStatus.NORMAL -> {
                myBitmapAnim.expanded()
                bottomTabView.showStatus(MapLayerBottomTabView.LayerStatus.NORMAL)
            }
        }
    }

    override fun onBackPressed() {
        val nowTime = System.currentTimeMillis()
        if (nowTime - previousTime > quiteTimeGap) {
            showToast("再按一次退出")
            previousTime = nowTime
        } else {
            finish()
            exitProcess(0)
        }
    }

//    @OnMessage("DralableLayoutState")
//    fun DralableLayoutState(b: Boolean) {
//        if (b) {
//            layerInfoNumState = mBinding.layerInfoNum.visibility
//            mBinding.layerInfoNum.visibility = View.GONE
//            mBinding.coordinator.visibility = View.GONE
//            mBinding.bottomTabView.visibility = View.GONE
//        } else {
//            if (layerInfoNumState != null) {
//                mBinding.layerInfoNum.visibility = layerInfoNumState!!
//                layerInfoNumState = null
//            }
//            mBinding.coordinator.visibility = View.VISIBLE
//            mBinding.bottomTabView.visibility = View.VISIBLE
//        }
//    }

}
